// 异步请求数据，等待加载的用法
export function getData() {
  return new Promise<number>((resolve, reject) => {
    setTimeout(() => {
      resolve(Math.random());
    }, 3000);
  });
}

export function fetchData() {
  let res: number;
  let status = "pending";
  const suspender = getData().then((data) => {
    res = data;
    status = "success";
  });

  return {
    read() {
      if (status === "success") return res;
      // 抛出一个 promise
      else throw suspender;
    },
  };
}

export default fetchData()
